<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>营业员管理</el-breadcrumb-item>
      <el-breadcrumb-item>职工表管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索&添加区域 -->
    <el-row :gutter="20" class="seacher">
      <el-col :span="8">
        <el-input placeholder="请输入内容" v-model="queryInfo.ename" clearable @clear="getEmployeeList">
          <el-button slot="append" icon="el-icon-search" @click="workerinfo"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="dialogFormVisible = true">添加职工</el-button>
      </el-col>
    </el-row>

    <!-- 添加弹出层 -->
    <el-dialog title="添加职工" :visible.sync="dialogFormVisible">
      <el-form :model="employee">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="employee.ename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地区" :label-width="formLabelWidth">
          <el-input v-model="employee.area" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="职位" :label-width="formLabelWidth">
          <el-input v-model="employee.position" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth">
          <el-input v-model="employee.department" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addWorker">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 表格区域 -->
    <el-table :data="getEmpList" border style="width: 100%" class="tab">
      <el-table-column prop="eid" label="职工号"> </el-table-column>
      <el-table-column prop="ename" label="姓名"> </el-table-column>
      <el-table-column prop="area" label="地区"> </el-table-column>
      <el-table-column prop="position" label="职位"> </el-table-column>
      <el-table-column prop="state" label="状态">
        <template slot-scope="scope">
          <el-tag v-show="scope.row.state === 0" type="success">{{ scope.row.state === 0 ? '在职' : '离职' }}</el-tag>
          <el-tag v-show="scope.row.state !== 0" type="danger">{{ scope.row.state === 0 ? '在职' : '离职' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="department" label="部门"> </el-table-column>
      <el-table-column prop="todo" label="操作">
        <template slot-scope="scope">
          <!-- 删除按钮 -->
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.eid)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
  </div>
</template>

<script>
export default {
  name: 'Worker',
  data() {
    return {
      // 获取员工表数据
      getEmpList: [],
      queryInfo: {
        ename: '',
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      employee: {
        eid: '',
        ename: '',
        area: '',
        position: '',
        department: '',
      },
      formLabelWidth: '120px',
      dialogFormVisible: false,
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      const { data: res } = await this.$http.get(`my/employee?pagenum=${this.queryInfo.pagenum}`)
      if (res.status !== 0) return this.$message.error(res.message)
      this.getEmpList = res.data
      this.total = res.total
    },
    async removeUserById(eid) {
      const confirmResult = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch((err) => err)

      if (confirmResult !== 'confirm') return

      const { data: res } = await this.$http.delete(`my/deleteEmployee/${eid}`)
      if (res.status !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getEmployeeList()
    },
    // 分页区的当前页数据显示的条数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getEmployeeList()
    },
    // 当前页面值
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getEmployeeList()
    },
    async workerinfo() {
      const { data: res } = await this.$http.get(`my/employee/info?ename=${this.queryInfo.ename}`)
      if (res.status !== 0) return this.$message.error(res.message)
      this.getEmpList = res.data
    },
    async addWorker() {
      let params = new URLSearchParams()
      params.append('ename', this.employee.ename)
      params.append('area', this.employee.area)
      params.append('position', this.employee.position)
      params.append('department', this.employee.department)
      const { data: res } = await this.$http.post('my/employee/updateInfo', params)
      if (res.status !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.dialogFormVisible = false
      this.getEmployeeList()
    },
  },
}
</script>

<style>
.seacher {
  margin-top: 20px;
}
</style>
